using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using Extra.Data.Extra.Class;
using Extra.Info;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace Extra.Data.Extra.UI
{
    public class XucData : XtraUserControl
    {
        private xfmAttachDatabase _frmAttach;
        private xfmCreateDatabase _frmCreateDatabase;
        private readonly xucDatabase _ucMain;
        private DevExpress.XtraBars.BarEditItem Bar;
        private DevExpress.XtraBars.BarEditItem Bar2;
        private DevExpress.XtraBars.BarStaticItem barStaticItem1;
        private DevExpress.XtraBars.BarStaticItem barStaticItem2;
        private DevExpress.XtraBars.BarButtonItem bbiAbout;
        private DevExpress.XtraBars.BarButtonItem bbiAttach;
        private DevExpress.XtraBars.BarButtonItem bbiBackup;
        private DevExpress.XtraBars.BarButtonItem bbiCreate;
        private DevExpress.XtraBars.BarButtonItem bbiDeattach;
        private DevExpress.XtraBars.BarButtonItem bbiDelete;
        private DevExpress.XtraBars.BarButtonItem bbiExit;
        private DevExpress.XtraBars.BarButtonItem bbiHelp;
        private DevExpress.XtraBars.BarButtonItem bbiLogin;
        private DevExpress.XtraBars.BarButtonItem bbiRefresh;
        private DevExpress.XtraBars.BarButtonItem bbiRestore;
        private DevExpress.XtraBars.BarButtonItem bbiUpgrade;
        private IContainer components = null;
        public readonly xfmLogin FormLogin;
        private DevExpress.Utils.ImageCollection incMain;
        private DevExpress.XtraBars.BarStaticItem lblDb;
        private PanelControl plMain;
        private DevExpress.XtraBars.Ribbon.RibbonControl rbcMain;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgAttach;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgBackup;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgClose;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgHelp;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgInit;
        private DevExpress.XtraBars.Ribbon.RibbonPageGroup rbpgUpgrade;
        private DevExpress.XtraBars.Ribbon.RibbonStatusBar rbStatus;
        private RepositoryItemMarqueeProgressBar repositoryItemMarqueeProgressBar1;
        private RepositoryItemProgressBar repositoryItemProgressBar1;
        private DevExpress.XtraBars.Ribbon.RibbonPage ribbonPage1;

        public XucData()
        {
            this.InitializeComponent();
            xucDatabase database = new xucDatabase
            {
                Dock = DockStyle.Fill
            };
            this._ucMain = database;
            this.plMain.Controls.Add(this._ucMain);
            this.Bar.Width = this.rbStatus.Width - 100;
            this.Bar2.Width = this.rbStatus.Width - 100;
            this._ucMain.DatabaseSelected += new xucDatabase.DatabaseSelectedEventHander(XucData.UcMainDatabaseSelected);
            this.FormLogin = new xfmLogin();
            base.SizeChanged += new EventHandler(XucData.XucDataSizeChanged);
        }

        private void AttachAttachComplete(object sender)
        {
            if (this.rbStatus.InvokeRequired)
            {
                AttachCompleteEventHander method = new AttachCompleteEventHander(this.AttachAttachComplete);
                base.Invoke(method, new object[] { sender });
            }
            else
            {
                this.Reload();
                if (this._frmAttach != null)
                {
                    this._frmAttach.Close();
                }
                this.bbiAttach.Enabled = true;
            }
        }

        private void AttachAttachError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                AttachErrorEventHander method = new AttachErrorEventHander(this.AttachAttachError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.bbiAttach.Enabled = true;
                XtraMessageBox.Show(message, "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                if (this._frmAttach != null)
                {
                    this._frmAttach.Enabled = true;
                    this._frmAttach.ShowDialog();
                }
            }
        }

        private void AttachDatabase(string db, string mdffile)
        {
            DataController controller = new DataController();
            controller.AttachComplete += new AttachCompleteEventHander(this.AttachAttachComplete);
            controller.AttachError += new AttachErrorEventHander(this.AttachAttachError);
            controller.Attach(db, mdffile);
        }

        private void BackupBackupComplete(object sender, ServerMessageEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                BackupCompleteEventHander method = new BackupCompleteEventHander(this.BackupBackupComplete);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.bbiBackup.Enabled = true;
                this.Bar.EditValue = 0;
            }
        }

        private void BackupBackupError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                BackupErrorEventHander method = new BackupErrorEventHander(this.BackupBackupError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                XtraMessageBox.Show(message, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.bbiBackup.Enabled = true;
            }
        }

        private void BackupBackupProcess(object sender, PercentCompleteEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                BackupProcessEventHander method = new BackupProcessEventHander(this.BackupBackupProcess);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.Bar.EditValue = e.Percent;
            }
        }

        private void BackupDataBase(string databaseName, string destinationPath)
        {
            DataController controller = new DataController();
            controller.BackupComplete += new BackupCompleteEventHander(this.BackupBackupComplete);
            controller.BackupError += new BackupErrorEventHander(this.BackupBackupError);
            controller.BackupProcess += new BackupProcessEventHander(this.BackupBackupProcess);
            controller.BackupDataBase(databaseName, destinationPath);
        }

        private void BbiAboutItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new frmAbout().ShowDialog();
        }

        private void BbiAttachItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this._frmAttach == null)
            {
                this._frmAttach = new xfmAttachDatabase(DataController.ConnectString);
            }
            this._frmAttach.AttachDatabase += new xfmAttachDatabase.AttachDatabaseEventHander(this.FrmAttachDatabase);
            this._frmAttach.FormClosing += new FormClosingEventHandler(this.FrmAttachFormClosing);
            this._frmAttach.ShowDialog();
        }

        private void BbiBackupItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog saveBackupDialog;
            string db = this._ucMain.Database;
            if ((db == null) | (db == ""))
            {
                XtraMessageBox.Show("Bạn chưa chọn dữ liệu để sao lưu!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                saveBackupDialog = new SaveFileDialog
                {
                    Filter = "Backup File(*.bak)|*.bak|All File(*.*)|*.*",
                    FilterIndex = 0
                };
                if (saveBackupDialog.ShowDialog() == DialogResult.OK)
                {
                    this.bbiBackup.Enabled = false;
                    new Thread(() => this.BackupDataBase(db, saveBackupDialog.FileName)).Start();
                }
            }
        }

        private void BbiCreateItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this._frmCreateDatabase == null)
            {
                this._frmCreateDatabase = new xfmCreateDatabase(DataController.ConnectString);
            }
            this._frmCreateDatabase.CreateDatabase += new xfmCreateDatabase.CreateDatabaseEventHander(this.FrmCreateDatabase);
            this._frmCreateDatabase.FormClosing += new FormClosingEventHandler(this.FrmCreateDatabaseFormClosing);
            this._frmCreateDatabase.ShowDialog();
        }

        private void BbiDeattachItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string db = this._ucMain.Database;
            if ((db == null) | (db == ""))
            {
                XtraMessageBox.Show("Bạn chưa chọn dữ liệu cần gỡ bỏ!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.bbiDeattach.Enabled = false;
                this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                new Thread(() => this.DetachDatabase(db)).Start();
            }
        }

        private void BbiDeleteItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string db;
            if (XtraMessageBox.Show("Bạn c\x00f3 chắc sẽ x\x00f3a dữ liệu n\x00e0y kh\x00f4ng?", "Cảnh B\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                db = this._ucMain.Database;
                if ((db == null) | (db == ""))
                {
                    XtraMessageBox.Show("Bạn chưa chọn dữ liệu để x\x00f3a!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else if (XtraMessageBox.Show("Dữ liệu (" + db + ") sẽ bị x\x00f3a ra khỏi hệ thống!", "Cảnh B\x00e1o", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand) != DialogResult.Cancel)
                {
                    this.bbiDelete.Enabled = false;
                    this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    new Thread(() => this.DeleteDatabase(db)).Start();
                }
            }
        }

        private void BbiExitItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Environment.Exit(0);
        }

        private void BbiLoginItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Disable(true);
            this._ucMain.Clear();
            xfmLogin login = new xfmLogin();
            login.Logined += new xfmLogin.LoginedEventHander(this.FrmLogined);
            login.ShowDialog();
        }

        private void BbiRefreshItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.DatabaseReload();
        }

        private void BbiRestoreItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ThreadStart start = null;
            string db;
            OpenFileDialog openBackupDialog;
            if (XtraMessageBox.Show("Bạn c\x00f3 chắc phục hồi dữ liệu kh\x00f4ng?", "Cảnh B\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No)
            {
                db = this._ucMain.Database;
                if ((db == null) | (db == ""))
                {
                    XtraMessageBox.Show("Bạn chưa chọn dữ liệu để phục hồi!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
                else if (XtraMessageBox.Show("Dữ liệu (" + db + ") sẽ được phục hồi, tất cả dữ liệu cũ sẽ bị x\x00f3a hết!", "Cảnh B\x00e1o", MessageBoxButtons.OKCancel, MessageBoxIcon.Hand) != DialogResult.Cancel)
                {
                    openBackupDialog = new OpenFileDialog
                    {
                        Filter = "Backup File(*.bak)|*.bak|All File(*.*)|*.*",
                        FilterIndex = 0
                    };
                    if (openBackupDialog.ShowDialog() == DialogResult.OK)
                    {
                        this.bbiRestore.Enabled = false;
                        if (start == null)
                        {
                            start = () => this.RestoreDataBase(openBackupDialog.FileName, db);
                        }
                        new Thread(start).Start();
                    }
                }
            }
        }

        private void BbiUpgradeItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            XtraMessageBox.Show("Vui l\x00f2ng v\x00e0o Website www.perfect.com.vn để cập nhật!", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        private void CreateDataBase(string db, string path)
        {
            string source = Application.StartupPath + @"\data.data";
            string desc = path;
            string database = db;
            DataController controller = new DataController();
            controller.CreatedProcess += new CreatedProcessEventHander(this.DataControllerCreatedProcess);
            controller.CreatedComplete += new CreatedCompleteEventHander(this.DataControllerCreatedComplete);
            controller.CreatedError += new CreatedErrorEventHander(this.DataControllerCreatedError);
            controller.Created(source, desc, database);
        }

        public void DatabaseReload()
        {
            this.bbiRefresh.Enabled = false;
            this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            this._ucMain.Clear();
            this._ucMain.DatabaseReload += new DatabaseReloadEventHander(this.UcMainDatabaseReload);
            this._ucMain.DatabaseReloadComplete += new DatabaseReloadCompleteEventHander(this.UcMainDatabaseReloadComplete);
            new Thread(new ThreadStart(this.Reload)).Start();
        }

        private void DataControllerCreatedComplete(object sender, ServerMessageEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                CreatedCompleteEventHander method = new CreatedCompleteEventHander(this.DataControllerCreatedComplete);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.Bar.EditValue = 0;
                if (!this.bbiCreate.Enabled)
                {
                    this.bbiCreate.Enabled = true;
                    this.DatabaseReload();
                    if (this._frmCreateDatabase != null)
                    {
                        this._frmCreateDatabase.Close();
                    }
                }
            }
        }

        private void DataControllerCreatedError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                CreatedErrorEventHander method = new CreatedErrorEventHander(this.DataControllerCreatedError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                XtraMessageBox.Show(message, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                this.bbiCreate.Enabled = true;
                this.Bar.EditValue = 0;
            }
        }

        private void DataControllerCreatedProcess(object sender, PercentCompleteEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                CreatedProcessEventHander method = new CreatedProcessEventHander(this.DataControllerCreatedProcess);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.Bar.EditValue = e.Percent;
            }
        }

        private void DeleteDatabase(string database)
        {
            DataController controller = new DataController();
            controller.DeletedComplete += new DeletedCompleteEventHander(this.DeletedDeletedComplete);
            controller.DeletedError += new DeletedErrorEventHander(this.DeletedDeletedError);
            controller.Deleted(database);
        }

        private void DeletedDeletedComplete(object sender)
        {
            this.Reload();
            this.bbiDelete.Enabled = true;
        }

        private void DeletedDeletedError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                DeletedErrorEventHander method = new DeletedErrorEventHander(this.DeletedDeletedError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                this.bbiDelete.Enabled = true;
                XtraMessageBox.Show("Error:\n\t" + message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void DetachDatabase(string database)
        {
            DataController controller = new DataController();
            controller.DetachComplete += new DetachCompleteEventHander(this.DetachDetachComplete);
            controller.DetachError += new DetachErrorEventHander(this.DetachDetachError);
            controller.Detach(database);
        }

        private void DetachDetachComplete(object sender)
        {
            if (this.rbStatus.InvokeRequired)
            {
                DetachCompleteEventHander method = new DetachCompleteEventHander(this.DetachDetachComplete);
                base.Invoke(method, new object[] { sender });
            }
            else
            {
                this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.bbiDeattach.Enabled = true;
                this.Reload();
            }
        }

        private void DetachDetachError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                DetachErrorEventHander method = new DetachErrorEventHander(this.DetachDetachError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.bbiDeattach.Enabled = true;
                XtraMessageBox.Show("Error:\n\t" + message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        private void Disable(bool disable)
        {
            this.rbpgInit.Visible = !disable;
            this.rbpgAttach.Visible = !disable;
            this.rbpgBackup.Visible = !disable;
            this.rbpgUpgrade.Visible = !disable;
            this.bbiRefresh.Visibility = !disable ? DevExpress.XtraBars.BarItemVisibility.Always : DevExpress.XtraBars.BarItemVisibility.Never;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FrmAttachDatabase(object sender, string db, string mdffile)
        {
            this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
            this._frmAttach.Hide();
            this.bbiAttach.Enabled = false;
            new Thread(() => this.AttachDatabase(db, mdffile)).Start();
        }

        private void FrmAttachFormClosing(object sender, FormClosingEventArgs e)
        {
            this._frmAttach = null;
        }

        private void FrmCreateDatabase(object sender, string database, string mPath)
        {
            this.bbiCreate.Enabled = false;
            if (this._frmCreateDatabase != null)
            {
                this._frmCreateDatabase.Enabled = false;
                this.Bar.EditValue = 0;
                new Thread(() => this.CreateDataBase(database, mPath)).Start();
            }
        }

        private void FrmCreateDatabaseFormClosing(object sender, FormClosingEventArgs e)
        {
            this._frmCreateDatabase = null;
        }

        private void FrmLogined(object sender, string connecstring)
        {
            DataController.ConnectString = connecstring;
            this.DatabaseReload();
        }

        private void FrmMainResize(object sender, EventArgs e)
        {
            this.Bar.Width = this.rbStatus.Width - 100;
            this.Bar2.Width = this.rbStatus.Width - 100;
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            ComponentResourceManager manager = new ComponentResourceManager(typeof(XucData));
            this.rbcMain = new DevExpress.XtraBars.Ribbon.RibbonControl();
            this.incMain = new DevExpress.Utils.ImageCollection(this.components);
            this.bbiCreate = new DevExpress.XtraBars.BarButtonItem();
            this.bbiDelete = new DevExpress.XtraBars.BarButtonItem();
            this.barStaticItem1 = new DevExpress.XtraBars.BarStaticItem();
            this.barStaticItem2 = new DevExpress.XtraBars.BarStaticItem();
            this.bbiExit = new DevExpress.XtraBars.BarButtonItem();
            this.bbiLogin = new DevExpress.XtraBars.BarButtonItem();
            this.bbiRefresh = new DevExpress.XtraBars.BarButtonItem();
            this.bbiAttach = new DevExpress.XtraBars.BarButtonItem();
            this.bbiDeattach = new DevExpress.XtraBars.BarButtonItem();
            this.bbiBackup = new DevExpress.XtraBars.BarButtonItem();
            this.bbiRestore = new DevExpress.XtraBars.BarButtonItem();
            this.bbiUpgrade = new DevExpress.XtraBars.BarButtonItem();
            this.bbiAbout = new DevExpress.XtraBars.BarButtonItem();
            this.bbiHelp = new DevExpress.XtraBars.BarButtonItem();
            this.Bar = new DevExpress.XtraBars.BarEditItem();
            this.repositoryItemProgressBar1 = new RepositoryItemProgressBar();
            this.Bar2 = new DevExpress.XtraBars.BarEditItem();
            this.repositoryItemMarqueeProgressBar1 = new RepositoryItemMarqueeProgressBar();
            this.lblDb = new DevExpress.XtraBars.BarStaticItem();
            this.ribbonPage1 = new DevExpress.XtraBars.Ribbon.RibbonPage();
            this.rbpgClose = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbpgInit = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbpgAttach = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbpgBackup = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbpgUpgrade = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbpgHelp = new DevExpress.XtraBars.Ribbon.RibbonPageGroup();
            this.rbStatus = new DevExpress.XtraBars.Ribbon.RibbonStatusBar();
            this.plMain = new PanelControl();
            this.rbcMain.BeginInit();
            this.incMain.BeginInit();
            this.repositoryItemProgressBar1.BeginInit();
            this.repositoryItemMarqueeProgressBar1.BeginInit();
            this.plMain.BeginInit();
            base.SuspendLayout();
            this.rbcMain.ApplicationIcon = null;
            this.rbcMain.Images = this.incMain;
            this.rbcMain.Items.AddRange(new DevExpress.XtraBars.BarItem[] { 
                this.bbiCreate, this.bbiDelete, this.barStaticItem1, this.barStaticItem2, this.bbiExit, this.bbiLogin, this.bbiRefresh, this.bbiAttach, this.bbiDeattach, this.bbiBackup, this.bbiRestore, this.bbiUpgrade, this.bbiAbout, this.bbiHelp, this.Bar, this.Bar2, 
                this.lblDb
             });
            this.rbcMain.LargeImages = this.incMain;
            this.rbcMain.Location = new Point(0, 0);
            this.rbcMain.MaxItemId = 0x1f;
            this.rbcMain.Name = "rbcMain";
            this.rbcMain.Pages.AddRange(new DevExpress.XtraBars.Ribbon.RibbonPage[] { this.ribbonPage1 });
            this.rbcMain.RepositoryItems.AddRange(new RepositoryItem[] { this.repositoryItemProgressBar1, this.repositoryItemMarqueeProgressBar1 });
            this.rbcMain.SelectedPage = this.ribbonPage1;
            this.rbcMain.ShowCategoryInCaption = false;
            this.rbcMain.ShowPageHeadersMode = DevExpress.XtraBars.Ribbon.ShowPageHeadersMode.Hide;
            this.rbcMain.ShowToolbarCustomizeItem = false;
            this.rbcMain.Size = new Size(0x284, 0x5f);
            this.rbcMain.StatusBar = this.rbStatus;
            this.rbcMain.Toolbar.ShowCustomizeItem = false;
            this.rbcMain.ToolbarLocation = DevExpress.XtraBars.Ribbon.RibbonQuickAccessToolbarLocation.Hidden;
            this.incMain.ImageSize = new Size(0x20, 0x20);
            this.incMain.ImageStream = (ImageCollectionStreamer)manager.GetObject("incMain.ImageStream");
            this.incMain.Images.SetKeyName(0, "Xp-MadB 004.png");
            this.incMain.Images.SetKeyName(1, "ark2.png");
            this.incMain.Images.SetKeyName(2, "ark.png");
            this.incMain.Images.SetKeyName(3, "Dang Ky.png");
            this.incMain.Images.SetKeyName(4, "database_warning.png");
            this.incMain.Images.SetKeyName(5, "databasebackup.png");
            this.incMain.Images.SetKeyName(6, "databaserestore.png");
            this.incMain.Images.SetKeyName(7, "db_status.png");
            this.incMain.Images.SetKeyName(8, "Exit1.png");
            this.incMain.Images.SetKeyName(9, "exit2.png");
            this.incMain.Images.SetKeyName(10, "exit.png");
            this.incMain.Images.SetKeyName(11, "Go Bo.png");
            this.incMain.Images.SetKeyName(12, "kthememgr.png");
            this.incMain.Images.SetKeyName(13, "May chu.png");
            this.incMain.Images.SetKeyName(14, "Nap.png");
            this.incMain.Images.SetKeyName(15, "Phuc Hoi.png");
            this.incMain.Images.SetKeyName(0x10, "Sao Luu.png");
            this.incMain.Images.SetKeyName(0x11, "StockPNG.png");
            this.incMain.Images.SetKeyName(0x12, "Tao.png");
            this.incMain.Images.SetKeyName(0x13, "Update.png");
            this.incMain.Images.SetKeyName(20, "Web Database.png");
            this.incMain.Images.SetKeyName(0x15, "Windows Mail.png");
            this.incMain.Images.SetKeyName(0x16, "WorldOfWacraftSZ.png");
            this.incMain.Images.SetKeyName(0x17, "xoa.png");
            this.incMain.Images.SetKeyName(0x18, "info.png");
            this.incMain.Images.SetKeyName(0x19, "Help and Support.png");
            this.incMain.Images.SetKeyName(0x1a, "Thong Tin.png");
            this.bbiCreate.Caption = "Tạo";
            this.bbiCreate.Id = 0;
            this.bbiCreate.ImageIndex = 0x27;
            this.bbiCreate.LargeImageIndex = 0x12;
            this.bbiCreate.Name = "bbiCreate";
            this.bbiCreate.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiCreateItemClick);
            this.bbiDelete.Caption = "Xo\x00e1";
            this.bbiDelete.Id = 6;
            this.bbiDelete.LargeImageIndex = 0x17;
            this.bbiDelete.Name = "bbiDelete";
            this.bbiDelete.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiDeleteItemClick);
            this.barStaticItem1.Caption = "Phần mềm Quản l\x00fd B\x00e1n h\x00e0ng - Bản quyền 2009 \x00a9 C\x00f4ng ty Phần mềm Ho\x00e0n Hảo";
            this.barStaticItem1.Id = 11;
            this.barStaticItem1.Name = "barStaticItem1";
            this.barStaticItem1.TextAlignment = StringAlignment.Near;
            this.barStaticItem2.Alignment = DevExpress.XtraBars.BarItemLinkAlignment.Right;
            this.barStaticItem2.Caption = "http://www.perfect.com.vn";
            this.barStaticItem2.Id = 13;
            this.barStaticItem2.Name = "barStaticItem2";
            this.barStaticItem2.TextAlignment = StringAlignment.Near;
            this.bbiExit.Caption = "Kết Th\x00fac";
            this.bbiExit.Id = 14;
            this.bbiExit.LargeImageIndex = 10;
            this.bbiExit.Name = "bbiExit";
            this.bbiExit.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiExitItemClick);
            this.bbiLogin.Caption = "M\x00e1y Chủ";
            this.bbiLogin.Id = 15;
            this.bbiLogin.LargeImageIndex = 13;
            this.bbiLogin.Name = "bbiLogin";
            this.bbiLogin.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiLoginItemClick);
            this.bbiRefresh.Caption = "Nạp";
            this.bbiRefresh.Id = 0x10;
            this.bbiRefresh.LargeImageIndex = 14;
            this.bbiRefresh.Name = "bbiRefresh";
            this.bbiRefresh.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.bbiRefresh.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiRefreshItemClick);
            this.bbiAttach.Caption = "Đăng K\x00fd";
            this.bbiAttach.Id = 0x11;
            this.bbiAttach.LargeImageIndex = 0x10;
            this.bbiAttach.Name = "bbiAttach";
            this.bbiAttach.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiAttachItemClick);
            this.bbiDeattach.Caption = "Gỡ Bỏ";
            this.bbiDeattach.Id = 0x12;
            this.bbiDeattach.LargeImageIndex = 15;
            this.bbiDeattach.Name = "bbiDeattach";
            this.bbiDeattach.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiDeattachItemClick);
            this.bbiBackup.Caption = "Sao Lưu";
            this.bbiBackup.Id = 0x13;
            this.bbiBackup.LargeImageIndex = 5;
            this.bbiBackup.Name = "bbiBackup";
            this.bbiBackup.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiBackupItemClick);
            this.bbiRestore.Caption = "Phục Hồi";
            this.bbiRestore.Id = 20;
            this.bbiRestore.LargeImageIndex = 6;
            this.bbiRestore.Name = "bbiRestore";
            this.bbiRestore.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiRestoreItemClick);
            this.bbiUpgrade.Caption = "N\x00e2ng Cấp";
            this.bbiUpgrade.Id = 0x15;
            this.bbiUpgrade.LargeImageIndex = 0x13;
            this.bbiUpgrade.Name = "bbiUpgrade";
            this.bbiUpgrade.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiUpgradeItemClick);
            this.bbiAbout.Caption = "Th\x00f4ng Tin";
            this.bbiAbout.Id = 0x16;
            this.bbiAbout.LargeImageIndex = 0x1a;
            this.bbiAbout.Name = "bbiAbout";
            this.bbiAbout.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(this.BbiAboutItemClick);
            this.bbiHelp.Caption = "Trợ Gi\x00fap";
            this.bbiHelp.Id = 0x17;
            this.bbiHelp.LargeImageIndex = 0x19;
            this.bbiHelp.Name = "bbiHelp";
            this.Bar.Caption = "Tiến Tr\x00ecnh  ";
            this.Bar.Edit = this.repositoryItemProgressBar1;
            this.Bar.Id = 0x19;
            this.Bar.Name = "Bar";
            this.Bar.PaintStyle = DevExpress.XtraBars.BarItemPaintStyle.Caption;
            this.Bar.VisibleWhenVertical = true;
            this.Bar.Width = 200;
            this.repositoryItemProgressBar1.Name = "repositoryItemProgressBar1";
            this.repositoryItemProgressBar1.ShowTitle = true;
            this.Bar2.Caption = "Tiến Tr\x00ecnh  ";
            this.Bar2.Edit = this.repositoryItemMarqueeProgressBar1;
            this.Bar2.EditValue = "0";
            this.Bar2.Id = 0x1a;
            this.Bar2.Name = "Bar2";
            this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
            this.Bar2.Width = 200;
            this.repositoryItemMarqueeProgressBar1.EditValueChangedFiringMode = EditValueChangedFiringMode.Buffered;
            this.repositoryItemMarqueeProgressBar1.MarqueeAnimationSpeed = 50;
            this.repositoryItemMarqueeProgressBar1.Name = "repositoryItemMarqueeProgressBar1";
            this.repositoryItemMarqueeProgressBar1.ProgressAnimationMode = ProgressAnimationMode.Cycle;
            this.lblDb.Alignment = DevExpress.XtraBars.BarItemLinkAlignment.Right;
            this.lblDb.Id = 30;
            this.lblDb.ItemClickFireMode = DevExpress.XtraBars.BarItemEventFireMode.Immediate;
            this.lblDb.MergeType = DevExpress.XtraBars.BarMenuMerge.MergeItems;
            this.lblDb.Name = "lblDb";
            this.lblDb.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.SmallWithText;
            this.lblDb.TextAlignment = StringAlignment.Near;
            this.ribbonPage1.Groups.AddRange(new DevExpress.XtraBars.Ribbon.RibbonPageGroup[] { this.rbpgClose, this.rbpgInit, this.rbpgAttach, this.rbpgBackup, this.rbpgUpgrade, this.rbpgHelp });
            this.ribbonPage1.Name = "ribbonPage1";
            this.ribbonPage1.Text = "ConvertedFromBarManager";
            this.rbpgClose.ItemLinks.Add(this.bbiExit);
            this.rbpgClose.ItemLinks.Add(this.bbiLogin);
            this.rbpgClose.ItemLinks.Add(this.bbiRefresh);
            this.rbpgClose.Name = "rbpgClose";
            this.rbpgClose.ShowCaptionButton = false;
            this.rbpgInit.ItemLinks.Add(this.bbiCreate);
            this.rbpgInit.ItemLinks.Add(this.bbiDelete);
            this.rbpgInit.Name = "rbpgInit";
            this.rbpgInit.ShowCaptionButton = false;
            this.rbpgInit.Visible = false;
            this.rbpgAttach.ItemLinks.Add(this.bbiAttach);
            this.rbpgAttach.ItemLinks.Add(this.bbiDeattach);
            this.rbpgAttach.Name = "rbpgAttach";
            this.rbpgAttach.ShowCaptionButton = false;
            this.rbpgAttach.Visible = false;
            this.rbpgBackup.ItemLinks.Add(this.bbiBackup);
            this.rbpgBackup.ItemLinks.Add(this.bbiRestore);
            this.rbpgBackup.Name = "rbpgBackup";
            this.rbpgBackup.ShowCaptionButton = false;
            this.rbpgBackup.Visible = false;
            this.rbpgUpgrade.ItemLinks.Add(this.bbiUpgrade);
            this.rbpgUpgrade.Name = "rbpgUpgrade";
            this.rbpgUpgrade.ShowCaptionButton = false;
            this.rbpgUpgrade.Visible = false;
            this.rbpgHelp.ItemLinks.Add(this.bbiAbout);
            this.rbpgHelp.ItemLinks.Add(this.bbiHelp);
            this.rbpgHelp.Name = "rbpgHelp";
            this.rbpgHelp.ShowCaptionButton = false;
            this.rbStatus.ItemLinks.Add(this.Bar);
            this.rbStatus.ItemLinks.Add(this.Bar2);
            this.rbStatus.ItemLinks.Add(this.lblDb, false, "", "", true);
            this.rbStatus.Location = new Point(0, 0x1b5);
            this.rbStatus.Name = "rbStatus";
            this.rbStatus.Ribbon = this.rbcMain;
            this.rbStatus.Size = new Size(0x284, 0x18);
            this.plMain.BorderStyle = BorderStyles.NoBorder;
            this.plMain.Dock = DockStyle.Fill;
            this.plMain.Location = new Point(0, 0x5f);
            this.plMain.Name = "plMain";
            this.plMain.Size = new Size(0x284, 0x156);
            this.plMain.TabIndex = 2;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Controls.Add(this.plMain);
            base.Controls.Add(this.rbStatus);
            base.Controls.Add(this.rbcMain);
            this.LookAndFeel.SkinName = "Blue";
            base.Name = "XucData";
            base.Size = new Size(0x284, 0x1cd);
            base.Resize += new EventHandler(this.FrmMainResize);
            this.rbcMain.EndInit();
            this.incMain.EndInit();
            this.repositoryItemProgressBar1.EndInit();
            this.repositoryItemMarqueeProgressBar1.EndInit();
            this.plMain.EndInit();
            base.ResumeLayout(false);
        }

        private void Reload()
        {
            this._ucMain.Refesh(DataController.ConnectString);
        }

        private void RestoreDataBase(string backupFilePath, string destinationDatabaseName)
        {
            DataController controller = new DataController();
            controller.RestoreComplete += new RestoreCompleteEventHander(this.RestoreRestoreComplete);
            controller.RestoreProcess += new RestoreProcessEventHander(this.RestoreRestoreProcess);
            controller.RestoreError += new RestoreErrorEventHander(this.RestoreRestoreError);
            controller.RestoreDatabase(backupFilePath, destinationDatabaseName);
        }

        private void RestoreRestoreComplete(object sender, ServerMessageEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                RestoreCompleteEventHander method = new RestoreCompleteEventHander(this.RestoreRestoreComplete);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.Bar.EditValue = 0;
                this.bbiRestore.Enabled = true;
            }
        }

        private void RestoreRestoreError(object sender, string message)
        {
            if (this.rbStatus.InvokeRequired)
            {
                RestoreErrorEventHander method = new RestoreErrorEventHander(this.RestoreRestoreError);
                base.Invoke(method, new object[] { sender, message });
            }
            else
            {
                XtraMessageBox.Show("Error:\n\t" + message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                this.bbiRestore.Enabled = true;
                this.Bar.EditValue = 0;
            }
        }

        private void RestoreRestoreProcess(object sender, PercentCompleteEventArgs e)
        {
            if (this.rbStatus.InvokeRequired)
            {
                RestoreProcessEventHander method = new RestoreProcessEventHander(this.RestoreRestoreProcess);
                base.Invoke(method, new object[] { sender, e });
            }
            else
            {
                this.Bar.EditValue = e.Percent;
            }
        }

        private void UcMainDatabaseReload(object sender, int percent)
        {
            if (this.rbStatus.InvokeRequired)
            {
                DatabaseReloadEventHander method = new DatabaseReloadEventHander(this.UcMainDatabaseReload);
                base.Invoke(method, new object[] { sender, percent });
            }
            else
            {
                this.Bar.EditValue = percent;
            }
        }

        private void UcMainDatabaseReloadComplete(object sender, DataTable dt)
        {
            if (this.rbStatus.InvokeRequired)
            {
                DatabaseReloadCompleteEventHander method = new DatabaseReloadCompleteEventHander(this.UcMainDatabaseReloadComplete);
                base.Invoke(method, new object[] { sender, dt });
            }
            else
            {
                this._ucMain.SetData(dt);
                this.Bar.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                this.Bar2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                this.bbiRefresh.Enabled = true;
                this.Bar.EditValue = 0;
                this.Disable(false);
            }
        }

        private static void UcMainDatabaseSelected(object sender, string db)
        {
        }

        private static void XucDataSizeChanged(object sender, EventArgs e)
        {
        }
    }

}
